package com.coder.oj.admin.controller;

import com.coder.oj.admin.dto.ProblemDto;
import com.coder.oj.admin.mapstruct.ProblemMapstruct;
import com.coder.oj.admin.query.ProblemQuery;
import com.coder.oj.admin.service.ProblemService;
import com.coder.oj.admin.vo.ProblemVo;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.JwtToken;
import com.coder.oj.common.core.R;
import com.coder.oj.common.entity.Problem;
import com.coder.oj.common.mapper.ProblemMapper;
import com.coder.oj.common.validate.ValidationGroups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = {"题目管理"})
@RestController
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    @Autowired
    private ProblemMapper problemMapper;

    @Autowired
    private ProblemMapstruct problemMapstruct;

    @JwtToken
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public R<JsonPage<ProblemVo>> getProblemPage(ProblemQuery params) {
        return R.ok().setResult(problemService.getProblemPage(params));
    }

    @JwtToken
    @GetMapping
    @ApiOperation("题目详情")
    public R<ProblemVo> getProblemById(@RequestParam("id") Long id) {
        return R.ok().setResult(problemMapstruct.toVO(problemMapper.selectById(id)));
    }

    @JwtToken
    @PostMapping
    @ApiOperation("新增题目")
    public R saveProblem(@Validated(ValidationGroups.Insert.class) @RequestBody ProblemDto params) {
        problemMapper.insert(problemMapstruct.toEntity(params));
        return R.ok();
    }

    @JwtToken
    @PutMapping
    @ApiOperation("修改题目")
    public R updateProblem(@Validated(ValidationGroups.Update.class) @RequestBody ProblemDto params) {
        problemService.updateProblem(params);
        return R.ok();
    }

    @JwtToken
    @DeleteMapping
    @ApiOperation("删除题目")
    public R deleteProblem(@RequestParam("id") Long id) {
        problemService.deleteProblem(id);
        return R.ok();
    }
}
